package com.invest.downloadMD.utils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 爬csdn博客工具
 * create by zhangtao
 */
public class ClimbUtil {
    final static String CSDN_DEFAULTPATH="./csdn_posts/";
    public static void main(String[] args) {
        //   爬当前展示列所有文章
//        climb("https://blog.csdn.net/qq_45774645");
//        爬单个
//        String path ="https://blog.csdn.net/yunmuq/article/details/123687548";
        String path ="https://blog.csdn.net/weixin_45476104/article/details/135049062";
        climbOne(path,null);

    }

    //
    public static void climb(String url) {
//        if (url.contains("?")) {
//            url = url.substring(0, url.indexOf("?"));
//        }
        // 把下面这个base_url换成你csdn的地址
        String secondUrl = url + "/article/list/";
        // 创建文件夹
        File file = new File("./_posts/");
        if (!file.exists()) {
            file.mkdir();
        }
        for (int i = 1; ; i++) {
            // 从第一页开始爬取
            String startUrl = secondUrl + i;
            Document doc = null;
            try {
                doc = Jsoup.connect(startUrl).get();
            } catch (IOException e) {
                System.out.println("jsoup获取url失败" + e.getMessage());
            }
            Element element = doc.body();
            //找到div class='article-list'
            element = element.select("div.article-list").first();
            if (element == null) {
                break;
            }
            Elements elements = element.children();
            List<CompletableFuture<Void>> futureList = new LinkedList<>();
            for (Element e : elements) {
                final String finalUrl = url;
                CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
                    // 拿到文章id
                    String articleId = e.attr("data-articleid");
                    // 爬取单篇文章
                    climbDetailByUrl(finalUrl, articleId);
                });
                futureList.add(future);
            }
            //等待所有线程执行完毕
            CompletableFuture.allOf(futureList.toArray(new CompletableFuture[futureList.size()])).join();
        }
    }

    /**
     * 爬取CSDN单个文章
     * @param url
     */
    private static void climbOne(String url,String articlePath) {
        System.out.println("》》》》》》》爬虫开始《《《《《《《");
        articlePath=articlePath==null?CSDN_DEFAULTPATH:articlePath;
        File file = new File(articlePath);
        if (!file.exists()) {
            file.mkdir();
        }
        // 爬取单篇文章
        climbDetailByUrl(url,articlePath);
        System.out.println("》》》》》》》爬虫结束《《《《《《《");
    }


    /**
     *
     * @param startUrl 文章url
     * @param articlePath  本都存储路径
     */
    private static void climbDetailByUrl(String startUrl,String articlePath) {
        Document doc = null;
        try {
            doc = Jsoup.connect(startUrl).timeout(20000).get();
        } catch (IOException e) {
            System.out.println("jsoup获取url失败" + e.getMessage());
        }
        Element element = doc.body();

        Element htmlElement = element.select(".toc").first();
        String jekylltr=getAuthorMessage(element);
        String md = Html2Md.getMarkDownText(htmlElement);


        String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        String mdFileName = articlePath+"/"+date + getTilter(element) + ".md";
        md = jekylltr + md;
        FileWriter writer;
        try {
            writer = new FileWriter(mdFileName);
            writer.write(md);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 获取原文章信息
     * @param element
     * @return
     */
    private static String getAuthorMessage(Element element) {

        Element titleElement = element.selectFirst(".title-article");
        String fileName = titleElement.text();
        System.out.println(fileName);
        // title
        String jekyllTitle = "title:   " + fileName + "\n";
        // 分类
        Elements elements = element.select("div.tags-box");
        String categories = "tags:    " + getTagsBoxValue(elements);
        //作者
        Element authorElement = element.selectFirst("a.follow-nickName");
        String author = "author:  "+authorElement.text()+ "\n";
        // 获取时间
        String time = "";
        Element timeElement = element.selectFirst("span.time");
        String reg = "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(timeElement.text());
        if (matcher.find()) {
            time = matcher.group();
        }
        String jekyllDate = "date:   " + time + "\n";

        return "---\n" + jekyllTitle + jekyllDate + author + categories+ "\n---\n";
    }

    /**
     * 获取标题
     * @param element
     * @return
     */
    public static String getTilter(Element element){
        Element titleElement = element.selectFirst(".title-article");
        return titleElement.text();
    }


    /**
     * 获取文章标签
     * @param elements
     * @return
     */
    private static String getTagsBoxValue(Elements elements) {
        String tags="";
        Elements categories = elements.select("a.tag-link");
        for (Element e : categories) {
            String temp = e.text().replace("\t", "").replace("\n", "").replace("\r", "");
            tags +=temp + "  ";
        }
        return tags;
    }


}
